package com.itic.mall.mobile.goods.sku;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.appbase.framework.utils.NumericHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.system.goods.goodssku.persistence.model.GoodsSku;
import com.itic.mall.system.goods.goodssku.persistence.model.GoodsSkuExample;
import com.itic.mall.system.goods.goodssku.persistence.model.GoodsSkuExample.Criteria;
import com.itic.mall.system.goods.goodssku.service.GoodsSkuService;
import com.itic.mall.system.goods.goodsspu.persistence.model.GoodsSpu;
import com.itic.mall.system.goods.goodsspu.service.GoodsSpuService;

@Controller
@RequestMapping(value = "/mobile/goods/sku")
public class SkuInterface extends BaseMobileInterface{
	
	@Autowired
	private GoodsSkuService goodsSkuService;
	
	@Autowired
	private GoodsSpuService goodsSpuService;
	
	
	/**
	 * 形成订单时支付后商品库存减少或增加 ,参数:商品号，数量，计算
	 * @author 王文鑫
	 * @date 2018年1月10日 下午10:00:51
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getGoodsInventoryBySkuId")
	@ResponseBody
	public Map<String, Object> getCouponNumById(HttpServletRequest request, HttpServletResponse response,String SkuId,String compute,int num) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String result = ResponseCode.SERVER_ERROR.getCode();
		
		int spuNum = 0; //spu参数
		int skuNum = 0; //sku参数
		
		int SpuTotalInventorys = 0; //spu修改后的参数
		int SkuTotalInventorys = 0; //sku修改后的参数
		
		GoodsSpu goodsSpu = new GoodsSpu();
		GoodsSku goodsSku = new GoodsSku();
		
		//判断参数
		if(StringHelper.isNotBlank(SkuId) && StringHelper.isNotBlank(compute) &&  num > 0 ){
			 goodsSpu = goodsSpuService.get(SkuId); //根据skuId获取sku商品
			 goodsSku = goodsSkuService.get(SkuId); //根据skuId 获取spu商品
			 //判断对象
			 if(null != goodsSpu && null != goodsSku){
				 
				 skuNum = goodsSku.getTotalInventory();
				 spuNum = goodsSpu.getTotalInventorys();
				 
				 if("sub".equals(compute)){
					//判断商品当前属性的库存
					if(spuNum != 0 && skuNum != 0){
						//sku库存是否大于需要修改的参数
						if(NumberStringHelper.isGreaterInteger(spuNum, num) && NumberStringHelper.isGreaterInteger(skuNum, num)){
							SpuTotalInventorys = NumericHelper.subInteger(spuNum, num);//减法
							SkuTotalInventorys = NumericHelper.subInteger(skuNum, num);
							
							goodsSpu.setTotalInventorys(SpuTotalInventorys);//重新设置spu库存属性
							goodsSku.setTotalInventory(SkuTotalInventorys);//重新设置sku库存属性
							
							result = goodsSpuService.edit(goodsSpu);//修改spu信息
							result = goodsSkuService.edit(goodsSku);//修改sku信息
						}else{
							result = TOTALINVENTORYS_ERROR_MSG;//商品库存信息错误
						}
					}else{
						result = SKUTOTALINVENTORYS_NULL_ERROR_MSG;//商品库存信息为空
					}
					
				 }else if("add".equals(compute)){
					 
					SpuTotalInventorys = NumericHelper.addInteger(spuNum, num);//加法
				 	SkuTotalInventorys = NumericHelper.addInteger(skuNum, num);
					
					goodsSpu.setTotalInventorys(SpuTotalInventorys);//重新设置spu库存属性
					goodsSku.setTotalInventory(SkuTotalInventorys);//重新设置sku库存属性
					
					result = goodsSpuService.edit(goodsSpu);//修改spu信息
					result = goodsSkuService.edit(goodsSku);//修改sku信息
				 }
				
			 }else{
				 result = OBJECT_ERROR_MSG;//获取对象错误
			 }
		}else{
			result = PARAM_ERROR_MSG;//缺少参数
		}
		resultMap.put(IticConst.RESULT, result);
		return resultMap;
	}
	
	/**
	 * 商品换货 
	 * @author 王文鑫
	 * @date 2018年1月10日 下午11:26:31
	 * @param request
	 * @param response
	 * @param AddSkuId
	 * @param SubSkuId
	 * @param num
	 * @return
	 */
	@RequestMapping(value = "/GoodsBarter")
	@ResponseBody
	public Map<String, Object> GoodsBarter(HttpServletRequest request, HttpServletResponse response,String AddSkuId,String SubSkuId,int num) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String result = ResponseCode.SERVER_ERROR.getCode();
		
		int skuAddNum = 0; //sku加法参数
		int skuSubNum = 0; //sku减法参数
		
		int addGoodsSkuInventorys = 0; //增加sku参数
		int subGoodsSkuInventorys = 0; //减少sku参数
		
		GoodsSku subGoodsSku = new GoodsSku(); //减少商品
		GoodsSku addGoodsSku = new GoodsSku(); //增加商品
		
		if(StringHelper.isNotBlank(AddSkuId) && StringHelper.isNotBlank(SubSkuId) &&  num > 0 ){
			 subGoodsSku = goodsSkuService.get(SubSkuId); //减少商品
			 addGoodsSku = goodsSkuService.get(AddSkuId); //增加商品
			 
			//判断对象
			 if(null != subGoodsSku && null != addGoodsSku){
				 skuAddNum = addGoodsSku.getTotalInventory();
				 skuSubNum = subGoodsSku.getTotalInventory();
				 
				 if(skuSubNum != 0){
					//sku库存是否大于需要修改的参数
						if(NumberStringHelper.isGreaterInteger(skuSubNum, num)){
							addGoodsSkuInventorys = NumericHelper.addInteger(skuAddNum, num);
							subGoodsSkuInventorys = NumericHelper.subInteger(skuSubNum, num);
							
							addGoodsSku.setTotalInventory(addGoodsSkuInventorys);//重新设置spu库存属性
							subGoodsSku.setTotalInventory(subGoodsSkuInventorys);//重新设置sku库存属性
							
							result = goodsSkuService.edit(addGoodsSku);//修改spu信息
							result = goodsSkuService.edit(subGoodsSku);//修改sku信息
						}else{
							result = TOTALINVENTORYS_ERROR_MSG;//商品库存信息错误
						}
				 }else{
					 result = SKUTOTALINVENTORYS_NULL_ERROR_MSG;//商品库存信息为空
				 }
			 }else{
				 result = OBJECT_ERROR_MSG;//获取对象错误
			 }
		}else{
			result = PARAM_ERROR_MSG;//缺少参数
		}
		resultMap.put(IticConst.RESULT, result);
		return resultMap;
	}
	
	@RequestMapping(value = "/count")
	@ResponseBody
	public Map<String, Object> getSkuCount(HttpServletResponse response,String skuId) {
		Map<String, Object> resultMap = new HashMap<>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			if(StringUtils.isNotEmpty(skuId)) {
				int count =0;
				GoodsSku goodsSku =  this.goodsSkuService.get(skuId);
				if(goodsSku!=null)
					count =goodsSku.getTotalInventory()==null?0:goodsSku.getTotalInventory();
				errorCode = SUCCESS_ERROR_CODE;
				errorMsg = SUCCESS_ERROR_MSG;
				resultMap.put(DATA, count);
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("SkuInterface", "getSkuCount", e.toString(), "取消订单", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
}
